def _get_exc_msg(self, err):
    exctype, value, tb = err
    exc_msg = str(value)
    exc_full_path = f'{exctype.__module__}.{exctype.__name__}'
    if self.know_exceptions and isinstance(self.know_exceptions, dict):
        exc_msg = self.know_exceptions.get(exc_full_path, exc_msg)
    return exc_msg



def startTest(self, test: unittest.case.TestCase):
    """单个用例执行开始"""
    super().startTest(test)  # 调用父类方法
    test.result = TestCaseResult(test)  # 实例化用例结果对象来记录用例结果，并绑定用例的result属性
    self.testcase_results.append(test.result)  # 另外添加到所有的结果列表一份
    test.result.start_at = time.time()  # 记录用例开始时间
    if self.verbosity > 1:
        self._log(f'执行用例: {test.result.name}: {test.result.description}, 开始时间: {time_to_string(test.result.start_at)}')
def stopTest(self, test: unittest.case.TestCase) -> None:
    """单个用例结束"""
    test.result.end_at = time.time()  # 记录用例结束时间
    test.result.duration = test.result.end_at - test.result.start_at   # 记录用例持续时间
    # 由于output要从_restoreStdout获取，手动加入父类恢复输出流的方法
    test.result.output = self._restoreStdout()
    self._mirrorOutput = False  # 是否重定向输出流标志


import time
from logz import log  # 需要安装logz
def time_to_string(timestamp: float) -> str:
    """时间戳转时间字符串，便于日志中更易读"""
    time_array = time.localtime(timestamp)
    time_str = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
    return time_str

class TestResut(unittest.TestResult):
    def startTestRun(self):
        """整个执行开始"""
        self.start_at = time.time()  # 整个执行的开始时间
        if self.verbosity > 1:
            self._log(f'===== 测试开始, 开始时间: {time_to_string(self.start_at)} =====')
    def stopTestRun(self):
        """整个执行结束"""
        self.end_at = time.time()  # 整个执行的结束时间
        self.duration = self.end_at - self.start_at  # 整个执行的持续
        self.success = self.wasSuccessful()  # 整个执行是否成功
        if self.verbosity > 1:
            self._log(f'===== 测试结束, 持续时间: {self.duration}秒 =====')

if __name__ == '__main__':
    import unittest
    from l_unittest.UnitResult.result import TestResult


    class TestDemo(unittest.TestCase):
        def test_a(self):  # 可以添加更多的用例进行测试
            print('测试a')

    suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestDemo)
    runner = unittest.TextTestRunner(resultclass=TestResult)  # 使用定制的TestResult类
    result = runner.run(suite)
    print(result.summary)  # 输出result的字典格式数据，建议使用pprint输出，需要安装pprint
